package com.moyou.weichat.chat;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.chat.EMMessage;
import com.moyou.weichat.BaseActivity;
import com.moyou.weichat.R;
import com.moyou.weichat.bean.ChatTypeBean;
import com.moyou.weichat.dapter.BaseChatAdapter;
import com.moyou.weichat.listener.ChatEMMessageListener;
import com.moyou.weichat.listener.ChatMessageCallBack;
import com.moyou.weichat.listener.ChatSendTypeListener;
import com.moyou.weichat.listener.OnChoosePictureListener;
import com.moyou.weichat.utils.ChatUtils;
import com.moyou.weichat.utils.ConstantUtils;
import com.moyou.weichat.utils.ImageUtils;
import com.moyou.weichat.utils.InputUtils;
import com.moyou.weichat.utils.RecyclerViewHoper;
import com.moyou.weichat.utils.YCLTools;
import com.moyou.weichat.view.VoiceLayout;
import com.moyou.weichat.view.ui.QqEmoticonsKeyBoard;
import com.moyou.weichat.view.ui.SimpleCommonUtils;

import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/10/18 0018.
 */

public abstract class BaseChatActivity extends BaseActivity implements QqEmoticonsKeyBoard.BtnCameraInterface
        ,ChatMessageCallBack  ,ChatSendTypeListener ,OnChoosePictureListener {
    @Bind(R.id.QqEmoticonsKeyBoard)
    protected QqEmoticonsKeyBoard qqEmoticonsKeyBoard;
    @Bind(R.id.chat_list)
    protected  RecyclerView chatRecyclerView;
    @Bind(R.id.chat_name)
    protected TextView chat_name;
    protected VoiceLayout voiceLayout;
    protected String pashChatId; //用户的环信id
    protected int chatType; //区分聊天类型
    protected BaseChatAdapter baseChatAdapter;
    protected ChatEMMessageListener chatEMMessageListener; //接受消息的监听
    protected List<ChatTypeBean> chatTypeBeenList; //聊天源数据

    @Override
    protected int getLayout() {
        return R.layout.activity_chat;
    }

    @Override
    protected void initTitle() {
        pashChatId =  getIntent().getStringExtra("userNmae");
        chatType = getIntent().getIntExtra("chatType", ConstantUtils.CHAT_TYPE_ERRO);
        if ( chatType == ConstantUtils.CHAT_TYPE_SING ){
            //单聊
        }else if ( chatType == ConstantUtils.CHAT_TYPE_GROUP ){
            //群聊
        }
    }

    @Override
    protected void initListener() {
        YCLTools.getInstance().setOnChoosePictureListener(this);
        ChatUtils.getChatUtils(mContext).setChatSendTypeListener(this);
        qqEmoticonsKeyBoard.getBtnSend().setOnClickListener(view -> onCLlck(view)); //发送文本的监听
        qqEmoticonsKeyBoard.setApi(this);
        chatRecyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                qqEmoticonsKeyBoard.reset();
            }
        });
        EMClient.getInstance().chatManager().addMessageListener(chatEMMessageListener);
    }

    @Override
    protected void initEventAndData() {
        //设置地步的数据
        SimpleCommonUtils Qq = new SimpleCommonUtils();
        voiceLayout = new VoiceLayout(mContext);
        Qq.initEmoticonsEditText(qqEmoticonsKeyBoard.getEtChat());
        qqEmoticonsKeyBoard.setAdapter(Qq.getCommonAdapter(mContext, ChatUtils.getChatUtils(mContext).getEtChat(qqEmoticonsKeyBoard)));
        qqEmoticonsKeyBoard.addFuncView(QqEmoticonsKeyBoard.FUNC_TYPE_MENU, ChatUtils.getChatUtils(mContext).getMenuView(0,mContext));
        qqEmoticonsKeyBoard.addFuncView(QqEmoticonsKeyBoard.FUNC_TYPE_VOICE, voiceLayout);
        chatEMMessageListener = new ChatEMMessageListener(this); //设置监听消息的回调！！
    }

    //按钮监听
    @OnClick({R.id.title_left_view ,R.id.btn_send})
    public void onCLlck(View view){
        switch (view.getId()){
            case R.id.title_left_view:
                finish();
                break;
            case R.id.btn_send:
                //发送文本消息
                String content = qqEmoticonsKeyBoard.getEtChat().getText().toString();
                ChatUtils.getChatUtils(mContext).createTextContent(content , pashChatId ,chatType);
                qqEmoticonsKeyBoard.getEtChat().setText("");
                baseChatAdapter.refreshSelectLast();
                break;
        }
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    @Override
    protected void createPresenter() {}

    //选择图片的回调
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        YCLTools.getInstance().upLoadImage(requestCode,resultCode ,data);
    }

    @Override
    public void btnVoice() {}

    @Override
    public void isOpenCall() {}

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //接受消息在销毁activity 的时候需要移除
        EMClient.getInstance().chatManager().removeMessageListener(chatEMMessageListener);
    }
    //接收到消息
    @Override
    public void onMessageReceived(List<EMMessage> messages) {
        baseChatAdapter.refreshSelectLast();
    }
    //接受到透传消息
    @Override
    public void onCmdMessageReceived() {

    }
    //收到已读回执
    @Override
    public void onMessageRead() {

    }
    //收到已送达回执
    @Override
    public void onMessageDelivered() {

    }
    //消息被撤回
    @Override
    public void onMessageRecalled() {

    }
    //消息状态变动
    @Override
    public void onMessageChanged() {

    }

}
